package com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.dnkeeper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DomainResult {
    public static final String KEY_ADDRESSLIST = "addressList";
    public static final String KEY_ATN_CODE = "atnCode";
    public static final String KEY_CREATE_TIME = "createTime";
    public static final String KEY_TTL = "ttl";
    public static final String KEY_TYPE = "type";
    public static final String KEY_VALUE = "value";
    public static final String TYPE_A = "A";
    public static final String TYPE_AAAA = "AAAA";
    public static final String TYPE_CNAME = "CNAME";
    private List<Address> addressList = new ArrayList();
    private long createTime = System.currentTimeMillis();
    private int source;
    private String type = "A";

    public static class Address {
        private long ttl;
        private String type;
        private String value;

        public static final class Builder {
            private long ttl = 2147483647L;
            private String type;
            private String value;

            public Builder type(String str) {
                this.type = str;
                return this;
            }

            public Builder value(String str) {
                this.value = str;
                return this;
            }

            public Builder ttl(long j) {
                this.ttl = j;
                return this;
            }

            public Address build() {
                return new Address();
            }
        }

        private Address(Builder builder) {
            this.type = builder.type;
            this.value = builder.value;
            this.ttl = builder.ttl;
        }

        public String getType() {
            return this.type;
        }

        public String getValue() {
            return this.value;
        }

        public long getTtl() {
            return this.ttl;
        }

        public String toString() {
            return "Address{type='" + this.type + '\'' + ", value='" + this.value + '\'' + ", ttl=" + this.ttl + '}';
        }
    }

    public String getType() {
        return this.type;
    }

    public void setType(String str) {
        this.type = str;
    }

    public List<Address> getAddressList() {
        return this.addressList;
    }

    public void setAddressList(List<Address> list) {
        if (list == null) {
            this.addressList = new ArrayList();
        } else {
            this.addressList = list;
        }
    }

    public int getSource() {
        return this.source;
    }

    public void setSource(int i) {
        this.source = i;
    }

    public List<String> getIpList() {
        List<String> arrayList = new ArrayList(this.addressList.size());
        long currentTimeMillis = System.currentTimeMillis();
        for (Address address : this.addressList) {
            long j = currentTimeMillis - this.createTime;
            if ((-address.getTtl()) < j && j < address.getTtl()) {
                arrayList.add(address.getValue());
            }
        }
        return arrayList;
    }

    public boolean isTimeOut() {
        long currentTimeMillis = System.currentTimeMillis();
        for (Address address : this.addressList) {
            long j = currentTimeMillis - this.createTime;
            if ((-address.getTtl()) < j && j < address.getTtl()) {
                return false;
            }
        }
        return true;
    }

    public void addAddress(Address address) {
        this.addressList.add(address);
    }

    public void addAddress(List<Address> list) {
        this.addressList.addAll(list);
    }

    public long getCreateTime() {
        return this.createTime;
    }

    public void setCreateTime(long j) {
        this.createTime = j;
    }

    public boolean isEmpty() {
        return this.addressList.isEmpty();
    }

    public boolean isNewly(DomainResult domainResult) {
        return this.createTime > domainResult.getCreateTime();
    }

    public String toString() {
        return "DnsResult{type='" + this.type + '\'' + ", addressList=" + Arrays.toString(this.addressList.toArray()) + ", createTime=" + this.createTime + '}';
    }
}
